

#include "QGridGraphicsItem.h"


#define GRID_W		64
#define GRID_H		32

#define MAP_GRID_W					64
#define MAP_GRID_H					32

#define MAP_GRID_W_HALF				(MAP_GRID_W >> 1)
#define MAP_GRID_H_HALF				(MAP_GRID_H >> 1)

#define MAP_GRID_W_SHF				6
#define MAP_GRID_H_SHF				5

#define MAP_GRID_W_HALF_SHF			(MAP_GRID_W_SHF - 1)
#define MAP_GRID_H_HALF_SHF			(MAP_GRID_H_SHF - 1)

#define MAP_GRID_W_MASK				63
#define MAP_GRID_H_MASK				31


QGridGraphicsItem::QGridGraphicsItem(QGraphicsScene *scene, QGraphicsItem *parent /* = 0 */)
    : QGraphicsObject(parent)
{
    QRectF rtScene = scene->sceneRect();

    int rows = rtScene.height() / MAP_GRID_H;
    int cols = rtScene.width() / MAP_GRID_W;
    int halfW = rtScene.width() / 2;
    int halfH = rtScene.height() / 2;

    int y1, y2;
    int x = -MAP_GRID_W_HALF;
    int y = -MAP_GRID_H_HALF;
    int i = 0, j = 0;
    QGraphicsLineItem *item = 0;

    for (i = 0; i < cols; ++i)
    {
        x += MAP_GRID_W;
        y += MAP_GRID_H;

        y1 = y;
        y2 = (rtScene.width() - x) / 2;

        QPointF p1(0, y1);
        QPointF p2(x, 0);

        QPointF p3(rtScene.width(), y2);
        y1 = y2 + halfH;
        QPointF p4(0, y1);

        QLineF l1(p1, p2);
        item = new QGraphicsLineItem(l1, this);
        QLineF l2(p2, p3);
        item = new QGraphicsLineItem(l2, this);
        QLineF l3(p3, p4);
        item = new QGraphicsLineItem(l3, this);

        y1 = rtScene.height() - (x >> 1);
        y2 = rtScene.height() - ((rtScene.width() - x) / 2);
        QPointF p5(0, y1);
        QPointF p6(x, rtScene.height());

        QPointF p7(rtScene.width(), y2);
        y1 = y2 - halfH;
        QPointF p8(0, y1);

        QLineF l4(p5, p6);
        item = new QGraphicsLineItem(l4, this);
        QLineF l5(p6, p7);
        item = new QGraphicsLineItem(l5, this);
        QLineF l6(p7, p8);
        item = new QGraphicsLineItem(l6, this);
    }

    mBoundingRect = rtScene;

    scene->addItem(this);
}

QGridGraphicsItem::~QGridGraphicsItem()
{

}

QRectF QGridGraphicsItem::boundingRect() const
{
    return mBoundingRect;
}

QPainterPath QGridGraphicsItem::shape() const
{
    QPainterPath path;
    path.addRect(mBoundingRect);
    return path;
}

void QGridGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget /* = 0 */)
{

}

